package cn.ctodb.cms.service;

import cn.ctodb.cms.domain.CmsSite;
import cn.ctodb.cms.repository.CmsSiteRepository;
import cn.ctodb.cms.repository.search.CmsSiteSearchRepository;
import cn.ctodb.cms.service.dto.CmsSiteDTO;
import cn.ctodb.cms.service.mapper.CmsSiteMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Service;

import javax.inject.Inject;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * Service Implementation for managing CmsSite.
 */
@Service
@Transactional
public class CmsSiteService {

    private final Logger log = LoggerFactory.getLogger(CmsSiteService.class);
    
    @Inject
    private CmsSiteRepository cmsSiteRepository;

    @Inject
    private CmsSiteMapper cmsSiteMapper;

    @Inject
    private CmsSiteSearchRepository cmsSiteSearchRepository;

    /**
     * Save a cmsSite.
     *
     * @param cmsSiteDTO the entity to save
     * @return the persisted entity
     */
    public CmsSiteDTO save(CmsSiteDTO cmsSiteDTO) {
        log.debug("Request to save CmsSite : {}", cmsSiteDTO);
        CmsSite cmsSite = cmsSiteMapper.cmsSiteDTOToCmsSite(cmsSiteDTO);
        cmsSite = cmsSiteRepository.save(cmsSite);
        CmsSiteDTO result = cmsSiteMapper.cmsSiteToCmsSiteDTO(cmsSite);
        cmsSiteSearchRepository.save(cmsSite);
        return result;
    }

    /**
     *  Get all the cmsSites.
     *  
     *  @return the list of entities
     */
    @Transactional(readOnly = true) 
    public List<CmsSiteDTO> findAll() {
        log.debug("Request to get all CmsSites");
        List<CmsSiteDTO> result = cmsSiteRepository.findAll().stream()
            .map(cmsSiteMapper::cmsSiteToCmsSiteDTO)
            .collect(Collectors.toCollection(LinkedList::new));

        return result;
    }

    /**
     *  Get one cmsSite by id.
     *
     *  @param id the id of the entity
     *  @return the entity
     */
    @Transactional(readOnly = true) 
    public CmsSiteDTO findOne(Long id) {
        log.debug("Request to get CmsSite : {}", id);
        CmsSite cmsSite = cmsSiteRepository.findOne(id);
        CmsSiteDTO cmsSiteDTO = cmsSiteMapper.cmsSiteToCmsSiteDTO(cmsSite);
        return cmsSiteDTO;
    }

    /**
     *  Delete the  cmsSite by id.
     *
     *  @param id the id of the entity
     */
    public void delete(Long id) {
        log.debug("Request to delete CmsSite : {}", id);
        cmsSiteRepository.delete(id);
        cmsSiteSearchRepository.delete(id);
    }

    /**
     * Search for the cmsSite corresponding to the query.
     *
     *  @param query the query of the search
     *  @return the list of entities
     */
    @Transactional(readOnly = true)
    public List<CmsSiteDTO> search(String query) {
        log.debug("Request to search CmsSites for query {}", query);
        return StreamSupport
            .stream(cmsSiteSearchRepository.search(queryStringQuery(query)).spliterator(), false)
            .map(cmsSiteMapper::cmsSiteToCmsSiteDTO)
            .collect(Collectors.toList());
    }
}
